home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / 3dvect37.zip / LOADGIF.ASM < prev    next >
Assembly Source File  |  1994-06-22  |  15KB  |  607 lines

  1. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2. ;
  3. ; Filename     : Loadgif.asm
  4. ; Included from: Main Assembley Module
  5. ; Description  : Gif and LZW decoding routines.  Written based on Rich
  6. ;                Geldreich's QBasic version.
  7. ;
  8. ; Written by: John McCarthy
  9. ;             1316 Redwood Lane
  10. ;             Pickering, Ontario.
  11. ;             Canada, Earth, Milky Way (for those out-of-towners)
  12. ;             L1X 1C5
  13. ;
  14. ; Internet/Usenet:  BRIAN.MCCARTHY@CANREM.COM
  15. ;         Fidonet:  Brian McCarthy 1:229/15
  16. ;   RIME/Relaynet: ->CRS
  17. ;
  18. ; Home phone, (905) 831-1944, don't call at 2 am eh!
  19. ;
  20. ; John Mccarthy would really love to work for a company programming Robots
  21. ; or doing some high intensive CPU work.  Hint. Hint.
  22. ;
  23. ; Send me your protected mode source code!
  24. ; Send me your Objects!
  25. ; But most of all, Send me a postcard!!!!
  26. ;
  27. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  28.  
  29.            .386p
  30.            jumps
  31.  
  32. code32     segment para public use32
  33.            assume cs:code32, ds:code32
  34.  
  35. ; define externals
  36.  
  37.            include pmode.ext       ; protected mode externals
  38.            include file.ext
  39.            include macros.inc
  40.  
  41.            public _loadgif
  42.            public _decode_lzw
  43.            public _getdword
  44.            public _getword
  45.            public _getbyte
  46.            public _input
  47.            public _loadgif_auto
  48.  
  49. _input     dd 0
  50. datastream dd 0
  51. gifmem     dd 0
  52. palettemem dd 0
  53. numcolours dw 0  ; number of colours in GIF
  54. nopalette  db 0  ; global or local colour map
  55. background db 0  ; background indexer in GIF
  56. xlength    dw 0  ; x and y size of GIF
  57. ylength    dw 0
  58.  
  59. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  60. ;
  61. ; Loadgif - simple GIF decoder
  62. ; In:
  63. ;    EDX - location of free memory to put palette and decoded LZW string
  64. ;    EAX - stream input routine (In:ECX=len,EDX->buf, Out:EAX=len,CF=1 error)
  65. ; Out:
  66. ;   CF=1 - Error decoding file
  67. ;   CF=0 - File decoded succesfully
  68. ;    EBX - location of palette - if EBX = EDX, then there is no palette in GIF
  69. ;    ECX - length of decoded GIF   - might not equal x*y (should, but might not)
  70. ;    EDX - location of decoded GIF - first two words are x and y size
  71. ;     AX - number of colours in GIF
  72. ;
  73. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  74.  
  75. _loadgif:
  76.            mov _input,eax
  77.            mov gifmem,edx
  78.            mov palettemem,edx
  79.  
  80.            call _getdword
  81.            jc error_in_gif
  82.            cmp eax,"8FIG"   ; check GIF8
  83.            jne error_in_gif
  84.  
  85.            call _getword    ; skip "7a" part of GIF
  86.            jc error_in_gif
  87.  
  88.            call _getword    ; skip totalx
  89.            jc error_in_gif
  90.  
  91.            call _getword    ; skip totaly
  92.            jc error_in_gif
  93.  
  94.            call _getbyte    ; numcolours
  95.            jc error_in_gif
  96.  
  97.            push ax
  98.  
  99.            and al,7
  100.            mov cl,al
  101.            inc cl
  102.            mov ax,1
  103.            shl ax,cl
  104.            mov numcolours,ax
  105.  
  106.            pop ax
  107.            and al,128
  108.            xor al,128
  109.            mov nopalette,al
  110.  
  111.            call _getbyte
  112.            jc error_in_gif
  113.            mov background,al
  114.  
  115.            call _getbyte
  116.            jc error_in_gif
  117.            cmp al,0          ; ? "Bad screen descriptor in GIF":end
  118.            jne error_in_gif
  119.  
  120.            cmp nopalette,0
  121.            jne do05
  122.  
  123.            mov cx,numcolours
  124.            mov ax,3
  125.            mul cx            ; ax = numcolours*3
  126.            movzx ecx,ax
  127.            mov edx,gifmem
  128.            add gifmem,ecx
  129.            push ecx
  130.            push edx
  131.            call _readfile
  132.            pop esi
  133.            pop ecx
  134.            jc error_in_gif
  135. divloop:
  136.            shr byte ptr [esi],2 ; adjust palette from 8 bit to 6 bit
  137.            inc esi
  138.            loop divloop
  139. do05:
  140.            call _getbyte
  141.            jc error_in_gif
  142.            cmp al,44
  143.            je exitdo
  144.            cmp al,33
  145.            jne error_in_gif  ; ? "Unknown extension type":end
  146.  
  147.            call _getbyte
  148.            jc error_in_gif
  149. do10:
  150.            call _getbyte
  151.            jc error_in_gif
  152.            movzx ecx,al
  153.            jcxz do05
  154. do20:
  155.            push ecx
  156.            call _getbyte
  157.            pop ecx
  158.            jc error_in_gif
  159.            loop do20
  160.  
  161.            jmp do10
  162. exitdo:
  163.            call _getword     ; skip image left and top
  164.            jc error_in_gif
  165.            call _getword
  166.            jc error_in_gif
  167.  
  168.            call _getword
  169.            jc error_in_gif
  170.            mov xlength,ax
  171.            call _getword
  172.            jc error_in_gif
  173.            mov ylength,ax
  174.  
  175.            call _getbyte
  176.            jc error_in_gif
  177.            test al,128+64
  178.            jnz error_in_gif  ; ? "Can't handle local colormaps or interlaced GIFs":end
  179.  
  180.            mov edx,gifmem
  181.            mov ax,xlength    ; set x and y size as first two words in decoded file
  182.            mov [edx],ax
  183.            mov ax,ylength
  184.            mov [edx+2],ax
  185.  
  186.            add edx,4
  187.            mov eax,_input
  188.            call _decode_lzw
  189.  
  190.            mov edx,gifmem
  191.            mov ebx,palettemem
  192.            mov ax,numcolours
  193.            ret
  194.  
  195. error_in_gif:
  196.            stc
  197.            ret
  198.  
  199. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  200. ;
  201. ; Decode_LZW - simple LZW decoder
  202. ; In:
  203. ;    EDX - memory location for decoded file
  204. ;    EAX - stream input routine (In:ECX=len,EDX->buf, Out:EAX=len,CF=1 error)
  205. ; Out:
  206. ;   CF=1 - Error decoding file
  207. ;   CF=0 - File decoded succesfully
  208. ;    ECX - length of decoded file
  209. ;    EDX - memory location of decoded file
  210. ;
  211. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  212.  
  213. clearcode     dd 0
  214. eoscode       dd 0
  215. firstcode     dd 0
  216. nextcode      dd 0
  217. startmaxcode  dd 0
  218. maxcode       dd 0
  219. startcodesize dd 0
  220. codesize      dd 0
  221. curcode       dd 0
  222. lastcode      dd 0
  223. lastpixel     dd 0
  224. lastchar      dd 0
  225. stackpointer  dd 0
  226. savelomem     dd 0
  227. savehimem     dd 0
  228. codex         dd 0
  229.  
  230. bitsin        dd 0
  231. blocksize     dd 0
  232. blockpointer  dd 0
  233. decodemem     dd 0
  234. decodememsav  dd 0
  235.  
  236. prefix        dd 0
  237. suffix        dd 0
  238. outstack      dd 0
  239. ybase         dd 0
  240. workcode      dd 0
  241. spaces        db 256 dup (0)
  242.  
  243. _decode_lzw:
  244.            mov _input,eax
  245.            mov decodemem,edx
  246.            mov decodememsav,edx    ; save starting code location
  247.  
  248.            mov eax,_lomembase      ; save for deallocation of memory
  249.            mov savelomem,eax
  250.            mov eax,_himembase
  251.            mov savehimem,eax
  252.  
  253.            mov eax,4096*2*3        ; allocate some memory
  254.            call _getmem
  255.            jc error_in_decode      ; out of memory error
  256.            mov prefix,eax
  257.            add eax,4096*2
  258.            mov suffix,eax
  259.            add eax,4096*2
  260.            mov outstack,eax
  261.  
  262.            call init_decode
  263.            jc error_in_decode2
  264.  
  265.            call decode0
  266.            jc error_in_decode2
  267.  
  268.            mov edx,decodememsav
  269.            mov ecx,decodemem
  270.            sub ecx,edx
  271.            clc
  272.  
  273. error_in_decode2:
  274.            mov eax,savelomem       ; deallocate memory
  275.            mov _lomembase,eax
  276.            mov eax,savehimem
  277.            mov _himembase,eax
  278.  
  279.            ret
  280.  
  281. error_in_decode:
  282.            stc
  283.            ret
  284.  
  285. shiftout   dd 128
  286.            dd 64
  287.            dd 32
  288.            dd 16
  289.            dd 8
  290.            dd 4
  291.            dd 2
  292.            dd 1
  293.  
  294. powersof2  dd 1
  295.            dd 2
  296.            dd 4
  297.            dd 8
  298.            dd 16
  299.            dd 32
  300.            dd 64
  301.            dd 128
  302.            dd 256
  303.            dd 512
  304.            dd 1024
  305.            dd 2048
  306.  
  307. init_decode:
  308.            call _getbyte
  309.            jc error_in_decode
  310.            mov edx,eax
  311.            mov eax,powersof2[eax*4]
  312.            mov clearcode,eax
  313.            mov eoscode,eax
  314.            add eoscode,1
  315.            mov firstcode,eax
  316.            add firstcode,2
  317.            mov nextcode,eax
  318.            add nextcode,2
  319.            mov startcodesize,edx
  320.            inc startcodesize
  321.            mov codesize,edx
  322.            inc codesize
  323.            mov ebx,powersof2[edx*4+4]
  324.            dec ebx
  325.            mov startmaxcode,ebx
  326.            mov maxcode,ebx
  327.  
  328.            mov bitsin,0
  329.            mov blocksize,0
  330.            mov blockpointer,1
  331.  
  332.            ret
  333.  
  334. decode0:
  335.            call getcode
  336.            jc error_in_decode
  337.  
  338.            mov eax,codex
  339.            cmp eax,eoscode
  340.            je end_of_decode
  341.  
  342.            mov eax,codex
  343.            cmp eax,clearcode
  344.            jne else0
  345.  
  346.            mov ebx,firstcode
  347.            mov nextcode,ebx
  348.            mov ebx,startcodesize
  349.            mov codesize,ebx
  350.            mov ebx,startmaxcode
  351.            mov maxcode,ebx
  352.            call getcode
  353.            jc error_in_decode
  354.            mov eax,codex
  355.            mov curcode,eax
  356.            mov lastcode,eax
  357.            mov lastpixel,eax
  358.  
  359.            mov edx,decodemem
  360.            mov [edx],al
  361.            inc decodemem
  362.  
  363.            jmp level0
  364.  
  365. else0:
  366.            mov curcode,eax
  367.            mov stackpointer,0
  368.  
  369.            cmp eax,nextcode
  370.            ja error_in_decode
  371.            jne dowhile1
  372.  
  373.            mov ebx,lastcode
  374.            mov curcode,ebx
  375.  
  376.            mov ecx,stackpointer
  377.            mov ebx,lastpixel
  378.            mov edi,outstack
  379.            mov [edi+ecx*2],bx ; outstack(stackpointer)=lastpixel
  380.  
  381.            inc stackpointer
  382.  
  383. dowhile1:
  384.            mov ebx,curcode
  385.            cmp ebx,firstcode
  386.            jl doneloop1
  387.  
  388.            mov ebp,curcode
  389.            mov edi,suffix
  390.            mov bx,[ebp*2+edi]
  391.            mov ebp,stackpointer
  392.            mov edi,outstack
  393.            mov [edi+ebp*2],bx
  394.  
  395.            inc stackpointer
  396.  
  397.            mov ebp,curcode
  398.            mov edi,prefix
  399.            xor ebx,ebx
  400.            mov bx,[ebp*2+edi]
  401.            mov curcode,ebx
  402.  
  403.            jmp dowhile1
  404.  
  405. doneloop1:
  406.            mov ebx,curcode
  407.            mov lastpixel,ebx
  408.  
  409.            mov ebx,lastpixel
  410.            mov edi,decodemem
  411.            mov [edi],bl
  412.            inc decodemem
  413.  
  414.            mov ecx,stackpointer
  415.            dec ecx
  416.            cmp ecx,-1
  417.            je outfornext
  418.  
  419. fornextloop:
  420.            mov esi,outstack
  421.            mov bx,[esi+ecx*2]
  422.            mov edi,decodemem
  423.            mov [edi],bl
  424.            inc decodemem
  425.  
  426.            dec ecx
  427.            cmp ecx,-1
  428.            jne fornextloop
  429.  
  430. outfornext:
  431.            cmp nextcode,4096
  432.            jae endif2
  433.  
  434.            mov ebx,lastcode
  435.            mov ecx,nextcode
  436.            mov edi,prefix
  437.            mov [edi+ecx*2],bx
  438.            mov ebx,lastpixel
  439.            mov edi,suffix
  440.            mov [edi+ecx*2],bx
  441.  
  442.            inc nextcode
  443.  
  444.            cmp codesize,12
  445.            jae endif2
  446.  
  447.            mov ecx,nextcode
  448.            cmp ecx,maxcode
  449.            jbe endif2
  450.  
  451.            inc codesize
  452.            shl maxcode,1
  453.            inc maxcode
  454. endif2:
  455.  
  456.            mov ebx,codex
  457.            mov lastcode,ebx
  458.  
  459. level0:
  460.            mov eax,codex
  461.            cmp eax,eoscode
  462.            jne decode0
  463.  
  464. end_of_decode:
  465.            clc
  466.            ret
  467.  
  468. getcode:
  469.            cmp bitsin,0
  470.            jne nogetbuf
  471.  
  472.            call getbufferedbyte
  473.            jc error_in_decode
  474.  
  475.            mov lastchar,eax
  476.            mov bitsin,8
  477. nogetbuf:
  478.            mov edx,bitsin
  479.            mov ecx,shiftout[edx*4-4]
  480.            mov eax,lastchar
  481.            cdq
  482.            div ecx
  483.            mov workcode,eax
  484. dowhile3:
  485.            mov eax,codesize
  486.            cmp eax,bitsin
  487.            jle exitdo2
  488.  
  489.            call getbufferedbyte
  490.            jc error_in_decode
  491.  
  492.            mov lastchar,eax
  493.  
  494.            mov ecx,bitsin
  495.            mov ebx,powersof2[ecx*4]
  496.            mul ebx
  497.            or workcode,eax
  498.  
  499.            add bitsin,8
  500.            jmp dowhile3
  501.  
  502. exitdo2:
  503.            mov eax,codesize
  504.            sub bitsin,eax
  505.  
  506.            mov eax,maxcode
  507.            and eax,workcode
  508.            mov codex,eax
  509.  
  510.            clc
  511.            ret
  512.  
  513. getbufferedbyte:
  514.            mov eax,blockpointer
  515.            cmp eax,blocksize
  516.            jle endif3
  517.  
  518.            call _getbyte
  519.            jc error_in_decode
  520.  
  521.            mov blocksize,eax
  522.  
  523.            mov ecx,eax
  524.            mov edx,offset spaces
  525.            call _readfile
  526.  
  527.            mov blockpointer,1
  528. endif3:
  529.            xor eax,eax
  530.            mov ecx,blockpointer
  531.            mov al,spaces[ecx-1]
  532.            inc blockpointer
  533.            clc
  534.            ret
  535.  
  536. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  537. ;
  538. ; Getdword - get dword from open file
  539. ; In:
  540. ;    _INPUT - stream input routine (In:ECX=len,EDX->buf, Out:EAX=len,CF=1 error)
  541. ; Out:
  542. ;   CF=1 - Error reading file
  543. ;     EAX - ?
  544. ;   CF=0 - Read went fine
  545. ;     EAX - dword from file
  546. ;
  547. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  548.  
  549. _getdword:
  550.            mov edx,offset datastream
  551.            mov ecx,4
  552.            call [_input]
  553.            mov eax,datastream
  554.            ret
  555.  
  556. _getword:
  557.            mov edx,offset datastream
  558.            mov ecx,2
  559.            call [_input]
  560.            xor eax,eax
  561.            mov ax,word ptr datastream
  562.            ret
  563.  
  564. _getbyte:
  565.            mov edx,offset datastream
  566.            mov ecx,1
  567.            call [_input]
  568.            xor eax,eax
  569.            mov al,byte ptr datastream
  570.            ret
  571.  
  572. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  573. ;
  574. ; _LoadGIF_Auto - perform no-setup loading of a GIF file
  575. ; In:
  576. ;    EDX - ASCIIZ filename
  577. ; Out:
  578. ;   CF=1 - Error reading file
  579. ;    Regs - ?
  580. ;   CF=0 - Read went fine
  581. ;    EBX - location of palette - if EBX = EDX, then there is no palette in GIF
  582. ;    ECX - length of decoded GIF   - might not equal x*y (should, but might not)
  583. ;    EDX - location of decoded GIF - first two words are x and y size
  584. ;     AX - number of colours in GIF
  585. ;
  586. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  587.  
  588. _loadgif_auto:
  589.            call _openfile
  590.            jc error_in_code
  591.  
  592.            mov edx,_lomembase       ; shove it in lomem - you got a problem with that?
  593.            mov eax,offset _readfile
  594.            call _loadgif            ; file remains open and is decoded from disk to memory
  595.            jc error_in_code
  596.  
  597.            mov eax,ecx
  598.            call _getlomem
  599.            jc error_in_code         ; probably will crash if out of memory..
  600.  
  601.            call _closefile
  602. error_in_code:
  603.            ret
  604.  
  605. code32     ends
  606.            end
  607.